<template>
	<view>
		<view class="fenxiang">
			<view>
				<image class="imggg" mode="widthFix" src="/static/image/1.png" @click="downloadQRCode" />
				<view>二维码下载</view>
			</view>
			<view @click="toAppShare(2)">

				<image class="imggg" mode="widthFix" src="/static/image/2.png" />
				<view>分享微信</view>
			</view>
			<view @click="shareToWechat('timeline')">
				<image class="imggg" mode="widthFix" src="/static/image/3.png" />
				<view>分享朋友圈</view>
			</view>
			<view @click="saveToAlbum">
				<image class="imggg" mode="widthFix" src="/static/image/4.png" />
				<view>保存相册</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import html2canvas from 'html2canvas';
	import siteInfo from '@/siteinfo.js';
	export default {
		data() {
			return {}
		},
		async mounted() {
			this.$jweixin.initJssdk(() => {
				this.toAppShare()
			})
		},
		methods: {
			toAppShare(type = 1) {
				let {
					siteroot
				} = siteInfo
				let title = '邀请有礼'
				let summary = '邀请新人 获得福利'
				let url = siteroot.split('/index.php')[0]
				let href = `${url}/h5/?#/user/pages/fenxiao/fenliu`
				let imageUrl = ''
				console.log(type)
				if (type>1) {
					this.$jweixin.showOptionMenu()
					this.$jweixin.shareAppMessage(title, summary, href, imageUrl)
					this.$jweixin.shareTimelineMessage(title, href, imageUrl)
				} else {
					this.$jweixin.hideOptionMenu()
				}
			},
			// 初始化微信JS-SDK
			async initWechatSDK() {
				var that = this
				try {
					// 1. 从后端获取签名配置（需替换为你的API）
					// const res = await uni.request({
					//     url: 'https://your-api.com/wechat-signature',
					//     data: { url: window.location.href.split('#')[0] }
					// });
					let param = {
						url: window.location.href.split('#')[0]
					}
					let res = await that.$api.usernew.getWechatSDK(param);
					console.log('datadatadatadata', res)
					// that.shangji1 = data.openid

					// 2. 配置微信SDK
					wx.config({
						debug: false, // 生产环境关闭调试
						appId: res.data.appId,
						timestamp: res.data.timestamp,
						nonceStr: res.data.nonceStr,
						signature: res.data.signature,
						jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData']
					});

					// 3. 设置默认分享内容（可选）
					wx.ready(() => {
						this.setDefaultShareInfo();
					});
				} catch (error) {
					console.error('微信SDK初始化失败:', error);
				}
			},

			// 设置默认分享信息
			setDefaultShareInfo() {
				const shareInfo = {
					title: '分享标题',
					desc: '分享描述',
					link: window.location.href,
					imgUrl: 'https://your-domain.com/share-icon.png'
				};

				// 微信好友
				wx.updateAppMessageShareData(shareInfo);
				// 朋友圈
				wx.updateTimelineShareData({
					...shareInfo,
					title: shareInfo.desc // 朋友圈只显示title
				});
			},

			// 触发分享
			shareToWechat(type) {
				if (!wx.ready) {
					uni.showToast({
						title: '请稍后重试',
						icon: 'none'
					});
					return;
				}

				const shareData = {
					title: '快来领取红包！',
					desc: '分享得现金红包，立即领取',
					link: window.location.href,
					imgUrl: this.swiperList[this.current].image, // 使用当前轮播图
					success: () => {
						uni.showToast({
							title: '分享成功',
							icon: 'success'
						});
					},
					cancel: () => {
						uni.showToast({
							title: '分享已取消',
							icon: 'none'
						});
					}
				};

				if (type === 'friend') {
					wx.updateAppMessageShareData(shareData);
					wx.invoke('shareAppMessage', shareData); // 唤起原生分享界面
				} else {
					wx.updateTimelineShareData({
						...shareData,
						title: shareData.desc // 朋友圈只显示desc
					});
					wx.invoke('shareTimeline', shareData);
				}
			},

			downloadQRCode() {
				this.$emit("downloadQRCode")
			},
			async saveToAlbum() {
				this.$emit("save")
			},

		}
	}
</script>
<style scoped>
	.fenxiang {
		display: flex;
		justify-content: center;
		justify-items: center;
		width: 100vw;
		margin-top: 80upx;
	}

	.fenxiang>view {
		text-align: center;
		width: 170upx;
	}

	.fenxiang>view .imggg {
		width: 80upx;
		height: 80upx;
		margin: 0 auto;
	}
</style>